/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package comunicatorserver.mail;

import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;


import javax.mail.Message;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author rjsantos
 */
public class SendMails extends Thread{

    private String player;

    public SendMails(String arg1){
        this.player=arg1;
    }


    @Override
    public void run(){

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ComunicatorServerPU");
        javax.persistence.EntityManager entityManager1 = java.beans.Beans.isDesignTime() ? null : emf.createEntityManager();



        Query q= entityManager1.createNativeQuery("SELECT id, nome, mail, telemovel FROM mails");
        //Query q= entityManager1.createNativeQuery("SELECT id, nome, mail, telemovel FROM mails where id=1");



        List l=q.getResultList();

        Iterator it = l.iterator();

        InternetAddress[] ia = new InternetAddress[l.size()];

        int i=0;

        Vector ob;

        while(it.hasNext()){

            ob=(Vector) it.next();

            try {
                ia[i] = new InternetAddress(ob.get(2).toString(), ob.get(1).toString());
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
                System.err.println("\n\nSendMail.java:82:UnsupportedEncodingException: "+ex.getMessage());
            }

            i++;
        }



        try
        {

            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            Properties prop = new Properties();

            prop.put("mail.smtp.host", "mail.barreiras.com.pt");
           
            prop.put("mail.smtp.auth", "true");  // If you need to authenticate
            





            Session session = Session.getDefaultInstance(prop,new ForcedAuthenticator());
            //session.setDebug(true);

            MimeMessage msg = new MimeMessage(session);

            InternetAddress addressFrom = new InternetAddress("Barreiras ACS<acs@barreiras.com.pt>");
            msg.setFrom(addressFrom);

            InternetAddress addressTo = new InternetAddress("acs@barreiras.com.pt");
            msg.setRecipient(Message.RecipientType.TO,addressTo);
            msg.setRecipients(Message.RecipientType.BCC,ia);

            Date dt= new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
            String s = formatter.format(dt);



            msg.setSubject(player+ " ligou-se ao servidor barreirasac.servegame.com ["+s+"]");
            msg.addHeader("HEADER", "Razi header ");

            String smail="<html> <table  cellpadding=\"10\" cellspacing=\"5\" ><tbody><tr><td style=\"background-color: rgb(29, 31, 29);\"> <br> <img style=\"width: 193px; height: 81px;\" alt=\"\"  src=\"http://assault.cubers.net/imgs/headerLogo.png\"><font  style=\"text-align: center; background-color: rgb(230, 230, 230);\"><br> <span style=\"font-family: Arial;\">&nbsp;Junta-te ao jogo no servidor barreirasac.servegame.com&nbsp;</span> </font> <br> <br> <font  style=\"text-align: center; background-color: rgb(230, 230, 230);\"><br> <span style=\"font-family: Arial;\">&nbsp;<small>Para mais informações visita <a href=\"http://acs.barreiras.com.pt\">http://acs.barreiras.com.pt</a>&nbsp;</small></span> </font><br> <br> </td></tr></tbody></html> ";
            msg.setContent(smail, "text/html");

            Transport.send(msg, ia);

            msg=null;

        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("\n\nSendMail.java:144:Exception: "+e.getMessage());
        }



        entityManager1.close();
        emf.close();




        ob=null;
        it=null;
        l=null;
        q=null;
        emf=null;
        entityManager1=null;


    }

}
